package com.F09.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.F09.actionform.F0905ActionForm;
import com.F09.dao.FunctionLanguageDAO;
import com.action.BaseAction;
import com.config.Constants;
import com.exception.SelectException;
import com.info.FunctionLanguageInfo;
import com.util.BuildMessages;
import com.util.CommonTools;

/**
 * 
 * @file F0903Action.java
 * @author Liushimin
 * @since 2008-10-14
 * @version 1.0
 */
public class F0905Action extends BaseAction {

	/**
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		F0905ActionForm actionForm = (F0905ActionForm) form;
		FunctionLanguageDAO dao = new FunctionLanguageDAO();
		FunctionLanguageInfo info = new FunctionLanguageInfo();
		String operationType = actionForm.getOperationType();
		String strSubmit = "submit";
		String strQuery = "query";
		String strDetail="detail";
		String strList = "list";
		
		if(CommonTools.isNullString(actionForm.getSearchState())){
			actionForm.setSearchState(Constants.STATE_01);
		}
		try {
			/*
			if(strSubmit.equals(operationType)){
				info.setFunctionId(actionForm.getFUNCTIONID());
				info.setName(actionForm.getNAME());
				info.setLanguage(actionForm.getLANGUAGE());
				List list = new ArrayList();
				list = dao.selectByCode(info);
				if(list.isEmpty()){ //list == null || list.size() == 0   
					try{
						if("".equals(actionForm.getFUNCTIONID())){	
							dao.insert(info);
						}
						if(!("".equals(actionForm.getFUNCTIONID()))){	
							dao.update(info);
						}
						operationType=strQuery;
						actionForm.setErrorFlag("����ɹ�!!!");
					}catch(Exception e){
						actionForm.setErrorFlag("����ʧ��!!!");
						e.printStackTrace();
					}
				}else {	
					super.saveMessages(request, BuildMessages.build("common.message"));
					return mapping.findForward(strDetail);
				}
			}

			if(Constants.DELETE.equals(operationType)){
				try{
					dao.deleteByKey(CommonTools.null2String(actionForm.getFUNCTIONID()));
					operationType=strQuery;
					actionForm.setErrorFlag("ɾ��ɹ�!!!");
				}catch(Exception e){
					actionForm.setErrorFlag("ɾ��ʧ��!!!");
					e.printStackTrace();
				}
			}
			*/
			
			if(Constants.ADD.equals(operationType)){
				doAdd(actionForm,dao);
				
				return mapping.findForward(strDetail);	
			}
			
			if(Constants.SAVE_INSERT.equals(operationType)){
				if(doInsert(actionForm,dao)){	
					String url=mapping.findForward("doList").getPath();
		            ActionForward forward=new ActionForward(url+"?operationType=queryList&errorFlag="+actionForm.getErrorFlag());
		    		return forward;
				}else{
					return mapping.findForward("detail");					
				}
			}
			
			if(Constants.SAVE_UPDATE.equals(operationType)){
				if(doUpdate(actionForm,dao)){				
					String url=mapping.findForward("doList").getPath();
		            ActionForward forward=new ActionForward(url+"?operationType=queryList&errorFlag="+actionForm.getErrorFlag());
		    		return forward;
				}else{
					return mapping.findForward("detail");		
				}
			}
			if(Constants.EDIT.equals(operationType)){
				doEdit(actionForm,dao);
                return mapping.findForward("detail");
			}
			if(Constants.DELETE.equals(operationType)){
				if(doDelete(actionForm,dao)){				
					String url=mapping.findForward("doList").getPath();
		            ActionForward forward=new ActionForward(url+"?operationType=queryList");
		    		return forward;
				}else{
					return mapping.findForward("list");		
				}
			}
			if(Constants.BACK.equals(operationType)){
				return mapping.findForward(strList);
			}
			
			if(Constants.QUERYLIST.equals(operationType)){
				info.setLanguage(CommonTools.null2String(actionForm.getLANGUAGE()));
				actionForm.setList(dao.queryList(info));
				return mapping.findForward(strList);
			}
			if(Constants.INIT.equals(operationType)){
				actionForm.setCollectionList(dao.getCollectionList());
				return mapping.findForward(strList);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (dao != null) dao.disConnect();
		}
		return mapping.findForward(strList);
	}
	
	private boolean doDelete(F0905ActionForm actionForm,FunctionLanguageDAO dao){
		boolean flag=false;
		try {
			String id=CommonTools.null2String(actionForm.getID());
			flag=dao.delete(id, actionForm);
			actionForm.setCollectionList(dao.getCollectionList());	
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	private void doAdd(F0905ActionForm actionForm,FunctionLanguageDAO dao){
		try {
			
			actionForm.setLayOutType("add");
			actionForm.setFUNCTIONID("");
			actionForm.setNAME("");
			actionForm.setLANGUAGE("");
			actionForm.setCollectionList(dao.getCollectionList());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	private boolean doInsert(F0905ActionForm actionForm,FunctionLanguageDAO dao){
		boolean flag=false;
		FunctionLanguageInfo info=new FunctionLanguageInfo();
		try {
			
			info.setFunctionId(CommonTools.null2String(actionForm.getFUNCTIONID()));
			info.setName(CommonTools.null2String(actionForm.getNAME()));
			info.setLanguage(CommonTools.null2String(actionForm.getLANGUAGE()));
			flag=dao.insert(info,actionForm);	
		} catch (Exception e) {
			e.printStackTrace();
		}	
		return flag;
	}
	
	private boolean doUpdate(F0905ActionForm form,FunctionLanguageDAO dao){
		boolean flag=false;
		try {
			
			form.setErrorFlag("");
			FunctionLanguageInfo info=new FunctionLanguageInfo();
			info.setName(CommonTools.null2String(form.getNAME()));
			info.setFunctionId(CommonTools.null2String(form.getFUNCTIONID()));
			info.setLanguage(CommonTools.null2String(form.getLANGUAGE()));
			info.setId(CommonTools.null2String(form.getID()));
			flag=dao.update(info,form);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;	
	}
	
	private void  doEdit(F0905ActionForm actionForm,FunctionLanguageDAO dao) throws SelectException{
		try {
			
		    Map map = null;
		    List list = new ArrayList();
			list = dao.selectById(actionForm.getID());
			 for(int i = 0; i < list.size();i++){
				map = (Map)list.get(i);	
				actionForm.setID(CommonTools.null2String((String)map.get("ID")));
				actionForm.setFUNCTIONID(CommonTools.null2String((String)map.get("FUNCTIONID")));
				actionForm.setNAME(CommonTools.null2String((String)map.get("NAME")));
				actionForm.setLANGUAGE(CommonTools.null2String((String)map.get("LANGUAGE")));
				actionForm.setFUNCTIONNAME(CommonTools.null2String((String)map.get("FUNCTIONNAME")));
				actionForm.setLayOutType("");
				actionForm.setCollectionList(dao.getCollectionList());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
}
